package com.canaan.business.service.basic.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.basic.StorehouseAreaPicker;
import com.canaan.business.mapper.basic.StorehouseAreaPickerMapper;
import com.canaan.business.service.basic.IStorehouseAreaPickerService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * 库区拣货员Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-06
 */
@Service
public class StorehouseAreaPickerServiceImpl implements IStorehouseAreaPickerService {
    @Resource
    private StorehouseAreaPickerMapper storehouseAreaPickerMapper;

    /**
     * 查询库区拣货员
     *
     * @param id 库区拣货员主键
     * @return 库区拣货员
     */
    @Override
    public StorehouseAreaPicker selectStorehouseAreaPickerById(Long id) {
        return storehouseAreaPickerMapper.selectStorehouseAreaPickerById(id);
    }

    /**
     * 查询库区拣货员列表
     *
     * @param storehouseAreaPicker 库区拣货员
     * @return 库区拣货员
     */
    @Override
    public List<StorehouseAreaPicker> selectStorehouseAreaPickerList(StorehouseAreaPicker storehouseAreaPicker) {
        return storehouseAreaPickerMapper.selectStorehouseAreaPickerList(storehouseAreaPicker);
    }


    public List<StorehouseAreaPicker> storehouseAreaPickerListByAreaNo(String areaNo){
        StorehouseAreaPicker param = new StorehouseAreaPicker();
        param.setStorehouseArea(areaNo);
        return selectStorehouseAreaPickerList(param);
    }
    /**
     * 新增库区拣货员
     *
     * @param storehouseAreaPicker 库区拣货员
     * @return 结果
     */
    @Override
    public int insertStorehouseAreaPicker(StorehouseAreaPicker storehouseAreaPicker) {
        if(StringUtils.isBlank(storehouseAreaPicker.getNo())){
            storehouseAreaPicker.setNo(TableNoUtil.getNo(StorehouseAreaPicker.class));
        }
        storehouseAreaPicker.setCreateBy(SecurityUtils.getUsername());
        storehouseAreaPicker.setCreateTime(DateUtils.getNowDate());
        return storehouseAreaPickerMapper.insertStorehouseAreaPicker(storehouseAreaPicker);
    }

    /**
     * 批量新增库区拣货员
     *
     * @param storehouseAreaPickerList 库区拣货员列表
     * @return 结果
     */
    @Override
    public int batchInsertStorehouseAreaPicker(List<StorehouseAreaPicker> storehouseAreaPickerList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StorehouseAreaPicker storehouseAreaPicker : storehouseAreaPickerList){
            if(StringUtils.isBlank(storehouseAreaPicker.getNo())){
                storehouseAreaPicker.setNo(TableNoUtil.getNo(StorehouseAreaPicker.class));
            }
            storehouseAreaPicker.setCreateBy(username);
            storehouseAreaPicker.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<storehouseAreaPickerList.size();) {
            int endIndex = i+500;
            if (endIndex > storehouseAreaPickerList.size()) {
                endIndex = storehouseAreaPickerList.size();
            }
            insertNum = insertNum + storehouseAreaPickerMapper.batchInsertStorehouseAreaPicker(storehouseAreaPickerList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改库区拣货员
     *
     * @param storehouseAreaPicker 库区拣货员
     * @return 结果
     */
    @Override
    public int updateStorehouseAreaPicker(StorehouseAreaPicker storehouseAreaPicker) {
        storehouseAreaPicker.setUpdateBy(SecurityUtils.getUsername());
        storehouseAreaPicker.setUpdateTime(DateUtils.getNowDate());
        return storehouseAreaPickerMapper.updateStorehouseAreaPicker(storehouseAreaPicker);
    }

    /**
     * 批量修改库区拣货员
     *
     * @param storehouseAreaPickerList 库区拣货员列表
     * @return 结果
     */
    @Override
    public int batchUpdateStorehouseAreaPicker(List<StorehouseAreaPicker> storehouseAreaPickerList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StorehouseAreaPicker storehouseAreaPicker : storehouseAreaPickerList){
            storehouseAreaPicker.setUpdateBy(username);
            storehouseAreaPicker.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<storehouseAreaPickerList.size();) {
            int endIndex = i+500;
            if (endIndex > storehouseAreaPickerList.size()) {
                endIndex = storehouseAreaPickerList.size();
            }
            updateNum = updateNum + storehouseAreaPickerMapper.batchUpdateStorehouseAreaPicker(storehouseAreaPickerList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除库区拣货员
     *
     * @param ids 需要删除的库区拣货员主键集合
     * @return 结果
     */
    @Override
    public int deleteStorehouseAreaPickerByIds(Long[] ids) {
        return storehouseAreaPickerMapper.deleteStorehouseAreaPickerByIds(ids);
    }

    /**
     * 删除库区拣货员信息
     *
     * @param id 库区拣货员主键
     * @return 结果
     */
    @Override
    public int deleteStorehouseAreaPickerById(Long id) {
        return storehouseAreaPickerMapper.deleteStorehouseAreaPickerById(id);
    }

    /**
     * 查询库区拣货员
     *
     * @param no 编号
     * @return 库区拣货员
     */
    @Override
    public StorehouseAreaPicker selectStorehouseAreaPickerByNo(String no) {
        return storehouseAreaPickerMapper.selectStorehouseAreaPickerByNo(no);
    }

    /**
     * 批量删除库区拣货员
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteStorehouseAreaPickerByNos(String[] nos) {
        return storehouseAreaPickerMapper.deleteStorehouseAreaPickerByNos(nos);
    }

    /**
     * 根据库区编号删除库区拣货员
     *
     * @param areaNo 需要删除的库区编号
     * @return 结果
     */
    @Override
    public int deleteStorehouseAreaPickerByAreaNo(String areaNo){
        return storehouseAreaPickerMapper.deleteStorehouseAreaPickerByAreaNo(areaNo);
    }

    /**
     * 删除库区拣货员信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteStorehouseAreaPickerByNo(String no) {
        return storehouseAreaPickerMapper.deleteStorehouseAreaPickerByNo(no);
    }
}
